package com.ruoyi.app.controller;

import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.ums.entity.Member;
import com.ruoyi.ums.entity.MemberAddress;
import com.ruoyi.ums.service.IMemberAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "用户地址")
@RestController
@RequestMapping("/user/address")
public class UserAddressController extends BaseController {
    @Autowired
    private IMemberAddressService memberAddressService;

    @ApiOperation("获取用户地址")
    @RequestMapping(value = "/selectAddress", method = RequestMethod.GET)
    public ApiResult<List<MemberAddress>> selectAddress() {
        Member member = getMember();
        List<MemberAddress> list = memberAddressService.selectAddressListByMemberId(member.getId());
        return ApiResult.success(list);
    }

    @ApiOperation("新增用户地址")
    @RequestMapping(value = "/insertAddress", method = RequestMethod.POST)
    public ApiResult<Void> insertAddress(@RequestBody MemberAddress address) {
        Member member = getMember();
        address.setMemberId(member.getId());
        memberAddressService.insertAddress(address);
        return ApiResult.success();
    }

    @ApiOperation("修改用户地址")
    @RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
    public ApiResult<Void> updateAddress(@RequestBody MemberAddress address) {
        memberAddressService.updateAddress(address);
        return ApiResult.success();
    }

    @ApiOperation("根据ids删除用户地址")
    @RequestMapping(value = "/deleteAddress", method = RequestMethod.GET)
    public ApiResult<Void> deleteAddress(@RequestParam Long[] ids) {
        memberAddressService.deleteAddressByIds(ids);
        return ApiResult.success();
    }

    @ApiOperation("获取默认用户地址")
    @RequestMapping(value = "/getDefaultAddress", method = RequestMethod.GET)
    public ApiResult<MemberAddress> getDefaultAddress() {
        Member member = getMember();
        MemberAddress address = memberAddressService.selectAddressDefaultByMemberId(member.getId());
        return ApiResult.success(address);
    }
}
